#include "stm32f10x.h"                  // Device header

void Timer_Init(void)//定时器2初始化函数
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//初始化时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	//初始化GPIOA(TIM2的ETR引脚),若时钟源是内部时钟则不需要
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
		
//	TIM_InternalClockConfig(TIM2);//时钟源为RCC内部时钟
    TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0x0F);//时钟源为ETR外部时钟
	//(定时器2)(不分频)(极性不翻转)(滤波周期0x0f)
 
	//初始化时基单元
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;//时钟不分割，也即CK_PSC=72mhz
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数模式
	TIM_TimeBaseInitStructure.TIM_Period=10;//ARR
	TIM_TimeBaseInitStructure.TIM_Prescaler=0;//PSC
	//公式:计数器溢出频率CK_CNT_OV=CK_PSC/(PSC+1)/(ARR+1)
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0x00;//自动重装载，用不到
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
    //
	
	TIM_Cmd(TIM2,ENABLE);//使能定时器2

	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//使能定时器中断
	TIM_ClearFlag(TIM2,TIM_FLAG_Update);//清除中断标志位
	//配置中断向量控制器
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//2位响应优先级，2位抢占优先级
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel= TIM2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	

}

